package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.yhny.lykj.yuhannongye.Adapter.ShopLeftListAdapter;
import com.yhny.lykj.yuhannongye.Adapter.ShopRightListAdapter;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseFragment;
import com.yhny.lykj.yuhannongye.app.BaseRefreshListAdapter;
import com.yhny.lykj.yuhannongye.app.LSCGridLayoutManager;
import com.yhny.lykj.yuhannongye.app.LSCLinearLayoutManager;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.model.ShopListModel;
import com.yhny.lykj.yuhannongye.model.ShopTypeModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.CodeScan;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by leach on 2017/7/26.
 */

public class ShopFragment extends BaseFragment {
    //    分类
    private RecyclerView rightListView;
    private ShopRightListAdapter rightListAdapter;
    private ArrayList<ShopListModel.ListEntity> rightList;

    private RecyclerView leftListView;
    private ShopLeftListAdapter leftListAdapter;
    private ArrayList<ShopTypeModel.ListEntity> leftList;

    private ArrayList<ShopTypeModel.ListEntity> list = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shop, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.left_group).setOnClickListener(this);
        view.findViewById(R.id.left_text).setOnClickListener(this);
        view.findViewById(R.id.right_group).setOnClickListener(this);
        view.findViewById(R.id.right_text).setOnClickListener(this);
        view.findViewById(R.id.name_top_bar).setOnClickListener(this);
        view.findViewById(R.id.search).setOnClickListener(this);
        //        分类
        leftListView = (RecyclerView)view.findViewById(R.id.shop_type1);
        final LSCLinearLayoutManager layoutManager1 = new LSCLinearLayoutManager(getContext());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        leftListView.setLayoutManager(layoutManager1);
        leftListView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).sizeResId(R.dimen.item_divider_size).colorResId(R.color.colorDivider_line).build());
        leftList = new ArrayList<>();
        leftListAdapter = new ShopLeftListAdapter(leftList);
        leftListAdapter.setOnItemClickListener(new BaseRefreshListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                loadShop(position);
                leftListAdapter.setSelectedPosition(position);
            }
        });
        leftListView.setAdapter(leftListAdapter);
        //        分类
        rightListView = (RecyclerView)view.findViewById(R.id.shop_type2);
        final LSCGridLayoutManager layoutManager = new LSCGridLayoutManager(getContext(),3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rightListView.setLayoutManager(layoutManager);
        rightListView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).sizeResId(R.dimen.item_divider_size).colorResId(R.color.white).build());
        rightList = new ArrayList<>();
        rightListAdapter = new ShopRightListAdapter(rightList);
        rightListAdapter.setOnItemClickListener(new BaseRefreshListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(),ShopListActivity.class);
                intent.putExtra(SHOP_FROM,SHOP_LIST);
                intent.putExtra(SHOP_ID,rightList.get(position).goodsid);
                startActivity(intent);
            }
        });
        rightListView.setAdapter(rightListAdapter);
        DataManager.getData(MODEL_PROCESS_0, NetHelper.Shop.getShopType(), this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.name_top_bar:
            case R.id.search:{
                //搜索
                Intent intent = new Intent(getActivity(),SearchActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.left_group:
            case R.id.left_text:{
                //二维码
                CodeScan.startCodeScan(getActivity());
                break;
            }
            case R.id.right_group:
            case R.id.right_text:{
                //消息
                Intent intent = new Intent(getActivity(),MsgListActivity.class);
                startActivity(intent);
                break;
            }
        }
    }

    private void loadShop(int position){
        showProgressDialog(R.string.text_loading);
        DataManager.getData(MODEL_PROCESS_1, NetHelper.Shop.getShopList(leftList.get(position).id,1), this);
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId) {
            case MODEL_PROCESS_0:{
                //分类
                BaseModel baseModel = (BaseModel) model;
                if (isDataEmpty(baseModel)) {

                } else {
                    ShopTypeModel modelDataList = (ShopTypeModel) model;
                    if (modelDataList == null || modelDataList.list == null || modelDataList.list.isEmpty()) {
                    } else {
                        leftList.clear();
                        leftList.addAll(modelDataList.list);
                        leftListAdapter.notifyDataSetChanged();
                        loadShop(0);
                    }
                }
                dismissProgressDialog();

                break;
            }
            case MODEL_PROCESS_1: {

                //商品列表
                BaseModel baseModel = (BaseModel) model;
                if (isDataEmpty(baseModel)) {

                } else {
                    ShopListModel modelDataList = (ShopListModel) model;
                    if (modelDataList == null || modelDataList.data == null || modelDataList.data.isEmpty()) {

                    } else {
                        rightList.clear();
                        rightList.addAll(modelDataList.data);
                        rightListAdapter.notifyDataSetChanged();
                    }
                }
                dismissProgressDialog();
                break;
            }

        }
        return false;
    }
}
